package cn.iocoder.idata.module.idata.controller.admin.datacategory;

import cn.iocoder.idata.framework.common.pojo.CommonResult;
import cn.iocoder.idata.framework.common.pojo.PageResult;
import cn.iocoder.idata.framework.excel.core.util.ExcelUtils;
import cn.iocoder.idata.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.idata.module.idata.controller.admin.datacategory.vo.*;
import cn.iocoder.idata.module.idata.convert.datacategory.DataCategoryConvert;
import cn.iocoder.idata.module.idata.dal.dataobject.datacategory.DataCategoryDO;
import cn.iocoder.idata.module.idata.service.datacategory.DataCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.idata.framework.common.pojo.CommonResult.success;
import static cn.iocoder.idata.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 数据类型信息")
@RestController
@RequestMapping("/idata/data-category")
@Validated
public class DataCategoryController {

    @Resource
    private DataCategoryService dataCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建数据类型信息")
    //@PreAuthorize("@ss.hasPermission('idata:data-category:create')")
    public CommonResult<Long> createDataCategory(@Valid @RequestBody DataCategoryCreateReqVO createReqVO) {
        return success(dataCategoryService.createDataCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据类型信息")
    //@PreAuthorize("@ss.hasPermission('idata:data-category:update')")
    public CommonResult<Boolean> updateDataCategory(@Valid @RequestBody DataCategoryUpdateReqVO updateReqVO) {
        dataCategoryService.updateDataCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据类型信息")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('idata:data-category:delete')")
    public CommonResult<Boolean> deleteDataCategory(@RequestParam("id") Long id) {
        dataCategoryService.deleteDataCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据类型信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('idata:data-category:query')")
    public CommonResult<DataCategoryRespVO> getDataCategory(@RequestParam("id") Long id) {
        DataCategoryDO dataCategory = dataCategoryService.getDataCategory(id);
        return success(DataCategoryConvert.INSTANCE.convert(dataCategory));
    }

    @GetMapping("/list")
    @Operation(summary = "获得数据类型信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    //@PreAuthorize("@ss.hasPermission('idata:data-category:query')")
    public CommonResult<List<DataCategoryRespVO>> getDataCategoryList(@RequestParam("ids") Collection<Long> ids) {
        List<DataCategoryDO> list = dataCategoryService.getDataCategoryList(ids);
        return success(DataCategoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据类型信息分页")
    //@PreAuthorize("@ss.hasPermission('idata:data-category:query')")
    public CommonResult<PageResult<DataCategoryRespVO>> getDataCategoryPage(@Valid DataCategoryPageReqVO pageVO) {
        PageResult<DataCategoryDO> pageResult = dataCategoryService.getDataCategoryPage(pageVO);
        return success(DataCategoryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据类型信息 Excel")
    //@PreAuthorize("@ss.hasPermission('idata:data-category:export')")
    @OperateLog(type = EXPORT)
    public void exportDataCategoryExcel(@Valid DataCategoryExportReqVO exportReqVO,
                                        HttpServletResponse response) throws IOException {
        List<DataCategoryDO> list = dataCategoryService.getDataCategoryList(exportReqVO);
        // 导出 Excel
        List<DataCategoryExcelVO> datas = DataCategoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "数据类型信息.xls", "数据", DataCategoryExcelVO.class, datas);
    }

}
